package com.hake.util;

import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.List;

/**
 * @author h.wang
 * @date 2017-02-24 上午 10:52
 */
public class PostUtil {

    public static ErrorInfo testPost(String urlStr,String xmlInfo) {
        boolean flag = false;
        String respMsg = "";
        String respCode = "";
        String  hpdz = "";
        ErrorInfo errorInfo = new ErrorInfo();
        try {
            //请求
            System.out.println(xmlInfo);
            URL url = new URL(urlStr);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setDoOutput(true);
            con.setRequestProperty("Content-Type", "text/xml; charset=UTF-8;");

            OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
            out.write(new String(xmlInfo.getBytes("UTF-8")));
            out.flush();
            out.close();

            //如果是批量代付报盘接口，需要返回tranId用于报盘查询
            String tranCode = "";
            String tranId = "";
            Document requestDoc= DocumentHelper.parseText(xmlInfo);
            List<Element> tranCodeList=requestDoc.selectNodes("HFBankData/head/tranCode");
            for(Element ele:tranCodeList){
                tranCode = ele.getTextTrim();
            }

            if(tranCode.equals("0001")) {
                List<Element> tranIdList = requestDoc.selectNodes("HFBankData/head/tranId");
                for (Element ele : tranIdList) {
                    tranId = ele.getTextTrim();
                }
            }

            //响应
            StringBuffer responseXmlStr = new StringBuffer();
            InputStream is = con.getInputStream();
            BufferedReader buffer = new BufferedReader(new InputStreamReader(is,"utf-8"));

            String line = null;
            while ((line = buffer.readLine()) != null) {
                responseXmlStr.append(line);
            }

            System.out.println(responseXmlStr);
            Document doc =  DocumentHelper.parseText(responseXmlStr.toString());
            Element rootElt = doc.getRootElement(); // 获取根节点

            Iterator iterss = rootElt.elementIterator("body");
            while (iterss.hasNext()) {
                Element recordEless = (Element) iterss.next();
                respMsg = recordEless.elementTextTrim("respMsg"); // 拿到body节点下的子节点respMsg值
                respCode = recordEless.elementTextTrim("respCode"); // respCode

                hpdz = recordEless.elementTextTrim("hpdz");

                if(respCode.startsWith("0")){
                    flag = true;
                }

                if(StringUtils.isNotBlank(hpdz)){
                    errorInfo.setErrInfo(respCode,respMsg,hpdz);
                }else if(StringUtils.isNotBlank(tranId)){
                    errorInfo.setErrInfo(respCode,respMsg,tranId);
                }else{
                    errorInfo.setErrInfo(respCode,respMsg,flag);
                }
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }

        return errorInfo;
    }
}
